package org.dromara.third.convert.enterprise.validator;

import cn.dev33.satoken.annotation.SaIgnore;
import org.dromara.common.core.service.DeptService;
import org.dromara.common.core.service.UserService;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.convert.enterprise.dto.DeviceDTO;
import org.dromara.third.convert.enterprise.dto.EquipmentDTO;
import org.dromara.third.convert.util.ExistenceValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import java.util.List;

@Component
@SaIgnore
public class EquipmentValidator implements Validator {

    @Autowired
    private DeptService deptService;

    @Autowired
    private UserService userService;

    @Override
    public boolean supports(Class<?> clazz) {
        return  List.class.isAssignableFrom(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        List<EquipmentDTO> equipmentDTOS = (List<EquipmentDTO>) target;

        // 基础字段验证
        validateRequiredFields(equipmentDTOS, errors);

//        validateExistence(equipmentDTOS, errors);
    }

    /**
     * 验证必填字段
     */
    private void validateRequiredFields(List<EquipmentDTO> dtos, Errors errors) {
        dtos.forEach(dto -> {
            ValidateUtil.rejectIfNull(dto::getId, String.valueOf(ExceptionEnum.AuthException01.getCode()), "主键不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getSszz, String.valueOf(ExceptionEnum.AuthException01.getCode()), "所属设置不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getSbmc, String.valueOf(ExceptionEnum.AuthException01.getCode()), "设备名称不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getSbwh, String.valueOf(ExceptionEnum.AuthException01.getCode()), "设备位号不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getSsbm, String.valueOf(ExceptionEnum.AuthException01.getCode()), "所属部门不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getSbzc, String.valueOf(ExceptionEnum.AuthException01.getCode()), "设备配置不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getSblx, String.valueOf(ExceptionEnum.AuthException01.getCode()), "设备类型不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getTzsbNy, String.valueOf(ExceptionEnum.AuthException01.getCode()), "是否特种设备不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getTzsbfl, String.valueOf(ExceptionEnum.AuthException01.getCode()), "设备分类不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getAqfjNy, String.valueOf(ExceptionEnum.AuthException01.getCode()), "是否安全附件不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getAqfjfl, String.valueOf(ExceptionEnum.AuthException01.getCode()), "安全附件分类不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getSbdj, String.valueOf(ExceptionEnum.AuthException01.getCode()), "设备等级不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getSbzt, String.valueOf(ExceptionEnum.AuthException01.getCode()), "设备状态不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getSbxh, String.valueOf(ExceptionEnum.AuthException01.getCode()), "设备型号不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getScrq, String.valueOf(ExceptionEnum.AuthException01.getCode()), "生产日期不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getSynx, String.valueOf(ExceptionEnum.AuthException01.getCode()), "使用年限不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getAzwz, String.valueOf(ExceptionEnum.AuthException01.getCode()), "安装位置不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getAzrq, String.valueOf(ExceptionEnum.AuthException01.getCode()), "安装日期不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getQyrq, String.valueOf(ExceptionEnum.AuthException01.getCode()), "启用日期不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getWhbyzq, String.valueOf(ExceptionEnum.AuthException01.getCode()), "维保期限不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getWhbydw, String.valueOf(ExceptionEnum.AuthException01.getCode()), "维保单位不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getZrr, String.valueOf(ExceptionEnum.AuthException01.getCode()), "责任人不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getLxfs, String.valueOf(ExceptionEnum.AuthException01.getCode()), "联系方式不能为空", errors);
            ValidateUtil.rejectIfEmpty(dto::getDelFlag, String.valueOf(ExceptionEnum.AuthException01.getCode()), "删除标志不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getCreateTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "创建时间不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getUpdateTime, String.valueOf(ExceptionEnum.AuthException01.getCode()), "更新时间不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getCreateBy, String.valueOf(ExceptionEnum.AuthException01.getCode()), "创建人不能为空", errors);
            ValidateUtil.rejectIfNull(dto::getUpdateBy, String.valueOf(ExceptionEnum.AuthException01.getCode()), "更新人不能为空", errors);
        });
    }

    private void validateExistence(List<EquipmentDTO> dtos, Errors errors) {
        dtos.forEach(dto -> {
            ExistenceValidator.validateDeptExists(dto.getSsbm(), "所属部门", errors);
        });
    }
}
